fun is_older (x: (int*int*int), y: (int*int*int)) =
	if #1 x = #1 y
	then if #2 x = #2 y
		then #3 x < #3 y
		else #2 x < #2 y
	else #1 x < #1 y 

fun number_in_month (x: (int*int*int) list, y: int) = 
    if null x
    then 0
    else if #2 (hd x) = y
    	then 1 + number_in_month(tl x, y)
    	else number_in_month(tl x, y)

fun number_in_months (x: (int*int*int) list, y: int list) =
	if null y
	then 0
	else number_in_month(x, hd y) + number_in_months(x, tl y)
	
fun dates_in_month (x: (int*int*int) list, y: int) = 
	if null x then
		[]
	else if #2 (hd x) = y then
		(hd x)::dates_in_month(tl x, y)
	else
		dates_in_month(tl x, y)

fun dates_in_months (x: (int*int*int) list, y: int list) = 
	if null y then
		[]
	else
		dates_in_month(x, hd y) @ dates_in_months(x, tl y) 

fun get_nth(x: string list, y: int) = 
	if y = 1 then 
		hd x
	else
		get_nth(tl x, y - 1)

fun date_to_string(x: int*int*int) = 
	let val month = ["January", "February", "March", "April",
						"May", "June", "July", "August", "September", 
						"October", "November", "December"]
	in
		get_nth(month, #2 x)^" "^Int.toString(#3 x)^", "^Int.toString(#1 x)
	end

fun number_before_reaching_sum(x: int, y: int list) =
	if x - hd y <= 0 then
		0
	else
		1 + number_before_reaching_sum(x - hd y, tl y)

fun what_month(x: int) =
	let
		val days = [31,28,31,30,31,30,31,31,30,31,30,31]
	in
		1 + number_before_reaching_sum(x, days)
	end

fun month_range(x: int, y: int) =
	if x > y then
		[]
	else
		what_month(x) :: month_range(x + 1, y)

fun oldest(x: (int*int*int) list) = 
	if null x
	then NONE
	else 
		let val tl_ans = oldest(tl x)
		in
			if  isSome tl_ans andalso is_older(valOf tl_ans, hd x)
			then 
				tl_ans
			else
				SOME (hd x)
		end
